package com.atguigu.demojuc.chap08;

import java.util.concurrent.*;

/**
 * @author njg
 * @version 1.0
 * @date 2025/6/18 18:31
 */
public class ExecutorsDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /**
         * 实用工具类创建线程池，使用线程池执行异步操作
         */
        //1.采用Executors产生线程池
        //1.1创建单一线程-线程池对象
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        //ExecutorService executorService = Executors.newCachedThreadPool(); // 遇强则强 、请求越多创建的线程数越多
        //ExecutorService executorService = Executors.newFixedThreadPool(3); // 固定线程数
        //ScheduledExecutorService executorService = Executors.newScheduledThreadPool(3); //定时任务线程数


        //executor方法入参只能是Runnable 只适用于线程没有返回结果    executorService.executor
        //executorService.submit 方法入参可以使Runnable或callable 适用于线程有或者没有返回结果
        Future<String> future = executorService.submit(() -> {
            System.out.println(Thread.currentThread().getName() + "\t" + "任务执行");
            return "njg";
        });

        //获取子线程结果
        String result = future.get();
        System.out.println(Thread.currentThread().getName() + "\t" + result);

    }
}
